﻿using NewCodeAPI.IServices;
using NewCodeAPI.Models;

/*try
{
    using(NdlsDBContext ndlsContext= new NdlsDBContext())
    {
        //新增
        ndlsContext.Add<BaseCourse>(new BaseCourse()
        {
            Id = 2,
            Area="asd"
        });
        ndlsContext.SaveChanges();

        //查询
        BaseCourse baseCourse=ndlsContext.BaseCourses.OrderBy(c => c.Id).FirstOrDefault();

        //修改
        baseCourse.Id = 3;
        ndlsContext.SaveChanges();

        //删除
        ndlsContext.Remove<BaseCourse>(baseCourse);
        ndlsContext.SaveChanges();

    }

}
catch(Exception ex)
{
    Console.WriteLine(ex.ToString());
}
*/

namespace NewCodeAPI.Service
{
    public class BaseService :IBaseService
    {

        //删除基地
        public MsgInfo DeleteBase(int no, string area)
        {
            using (var context = new NdlsDBContext())
            {
                var _base = context.BaseCourses.FirstOrDefault(b => b.No == no && b.Area == area);
                if (_base == null)
                {
                    return new MsgInfo { Status = "Error", Msg = "没有该基地，无法删除" };
                }
                else
                {
                    try
                    {
                        context.BaseCourses.Remove(_base);
                        context.SaveChanges();
                        return new MsgInfo { Status = "Success", Msg = "基地删除成功" };
                    }
                    catch
                    {
                        return new MsgInfo { Status = "Error", Msg = "基地删除失败" };
                    }

                }
            }
        }

        //修改基地信息

        public MsgInfo ModifyBase(int no, string area, BaseCourse baseCourse)
        {
            using (var context = new NdlsDBContext())
            {
                var existingBase = context.BaseCourses.FirstOrDefault(b => b.No == no && b.Area == area);
                if (existingBase == null)
                {
                    return new MsgInfo { Status = "Error", Msg = "基地不存在" };
                }
                else
                {
                    try
                    {
                        // 更新基地信息
                        existingBase.Area = baseCourse.Area;
                        existingBase.AreaEnUs=baseCourse.AreaEnUs;
                        existingBase.AreaZhTw = baseCourse.AreaZhTw;
                        existingBase.TrainingBase = baseCourse.TrainingBase;
                        existingBase.TrainingBaseEnUs = baseCourse.TrainingBaseEnUs;
                        existingBase.TrainingBaseZhTw = baseCourse.TrainingBaseZhTw;
                        existingBase.Address = baseCourse.Address;
                        existingBase.AddressEnUs = baseCourse.AddressEnUs;
                        existingBase.AddressZhTw = baseCourse.AddressZhTw;

                        // 根据需要更新其他字段

                        context.SaveChanges(); // 保存更改到数据库

                        return new MsgInfo { Status = "Success", Msg = "更新成功" };
                    }
                    catch
                    {
                        return new MsgInfo { Status = "Error", Msg = "更新失败" };
                    }
                }
            }
        }

        //获取基地信息
        public List<MsgInfo> GetBaseInfo(string area)
        {
            using (var context = new NdlsDBContext())
            {
                var bases = context.BaseCourses.Where(b => b.Area == area).ToList();

                if (bases.Count == 0)
                {
                    return new List<MsgInfo> { new MsgInfo { Status = "Error", Msg = "没有该基地" } };
                }
                else
                {
                    List<MsgInfo> result = bases.Select(baseItem => new MsgInfo { Status = "Success", Msg = "有该基地", BaseInfo = baseItem }).ToList();
                    return result;
                }
            }
        }

        //添加基地信息
        public MsgInfo AddBase(BaseCourse baseCourse)
        {
            using (var context = new NdlsDBContext()) // 创建数据库上下文实例
            {
                var _base = context.BaseCourses.FirstOrDefault(b => b.Id == baseCourse.Id);
                if (_base != null)
                {
                    return new MsgInfo { Status = "Error", Msg = "基地已存在" };
                }
                else
                {
                    try
                    {
                        // 将用户对象添加到数据库
                        context.BaseCourses.Add(baseCourse);
                        context.SaveChanges(); // 保存更改到数据库
                        return new MsgInfo { Status = "Success", Msg = "添加成功" };
                    }
                    catch
                    {
                        return new MsgInfo { Status = "Error", Msg = "添加失败" };
                    }
                }

            }
        }
    }
}
